//
//  RBStockOrderDetailVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockOrderDetailVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBStockAddOrderVC.h"
#import "RBStockOrderInvoiceInfoVC.h"
#import "RBStockOrderResponse.h" //model
#import "RBCashierVC.h" //收银台

@interface RBStockOrderDetailVC ()

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) RBStockOrderDetailResponse *response;

@end

@implementation RBStockOrderDetailVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"库存订单详情";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    //
    [self.scrollView view_clear];
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        [view view_addView:[self shopView] withHeight:Zoom(43) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:[self lineView] withHeight:1 width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
        [view view_addView:[self goodsViewWithData:self.response.data] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(12) bottom:Zoom(12) left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(12) right:Zoom(12) centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        CGFloat padding = Zoom(17);
        [view view_addView:[self itemWithTitle:@"订单编号" des:self.response.data.order_number] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"下单时间" des:self.response.data.create_time] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        NSString *payTypeString = @""; //支付方式 0未支付 1微信 2支付宝 3余额 4好友代付
        switch ([self.response.data.pay_type intValue]) {
            case 0 : {
                payTypeString = @"未支付";
            }   break;
            case 1 : {
                payTypeString = @"微信支付";
            }   break;
            case 2 : {
                payTypeString = @"支付宝支付";
            }   break;
            case 3 : {
                payTypeString = @"余额支付";
            }   break;
            case 4 : {
                payTypeString = @"好友代付";
            }   break;
        }
        [view view_addView:[self itemWithTitle:@"支付方式" des:payTypeString] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        
        NSString *payTimeString = self.response.data.pay_time;
        [view view_addView:[self itemWithTitle:@"支付时间" des:payTimeString] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(12) right:Zoom(12) centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        CGFloat padding = Zoom(17);
        NSString *mailType = @""; //邮寄方式【1-快递 | 2-自提】
        if ([self.response.data.mail_type isEqualToString:@"1"]) {
            mailType = @"快递";
        } else if ([self.response.data.mail_type isEqualToString:@"2"]) {
            mailType = @"自提";
        }
        [view view_addView:[self itemWithTitle:@"配送方式" des:mailType] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        
        [view view_addView:({
            UILabel *address = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"收货地址"];
            address;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:Zoom(15) right:0 centerX:0];
        
        NSString *namePhone = [NSString stringWithFormat:@"%@ %@", self.response.data.consignee_name, self.response.data.consignee_phone];
        [view view_addView:({
            UILabel *address = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:namePhone];
            address;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:Zoom(15) right:0 centerX:0];
        
        [view view_addView:({
            UILabel *address = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:self.response.data.full_address];
            address;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:Zoom(15) right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(12) right:Zoom(12) centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        CGFloat padding = Zoom(17);
        [view view_addView:[self itemWithTitle:@"商品总额" des:[NSString stringWithFormat:@"¥%@", self.response.data.original_price]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"运费" des:[NSString stringWithFormat:@"+¥%@", self.response.data.total_freight]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"商品优惠" des:[NSString stringWithFormat:@"-¥%@", self.response.data.total_coupon_price]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"实付金额" des:[NSString stringWithFormat:@"¥%@", self.response.data.total_price]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(12) right:Zoom(12) centerX:0];
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"order_id"] = self.order_id;
    } path:_Order_detail post:YES log:YES responseClass:RBStockOrderDetailResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBStockOrderDetailResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:^{
        [weakSelf getData];
    }];
}

#pragma mark - Event
//支付
- (void)goPayAction {
    RBOrderDetailModel *model = self.response.data;
    RBCashierVC *vc = RBCashierVC.new;
    vc.isFromOrder = YES;
    vc.order_id = self.order_id;
    vc.order_num = model.order_number;
    vc.order_price = model.total_price;
    LCWeakSelf;
    vc.block = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(paySuccessRefreshListAction)]) {
            [weakSelf.delegate paySuccessRefreshListAction];
        }
        [weakSelf getData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)addOrderBtnTapped
{
    RBAddOrderSubmitModel *pModel = RBAddOrderSubmitModel.new;
    pModel.parent_id = self.order_id;
    pModel.goods_id = self.response.data.goods_id;
    pModel.total_num = self.response.data.quantity;
    pModel.order_num = self.response.data.order_number;
    
    RBStockAddOrderVC *vc = RBStockAddOrderVC.new;
    vc.submitModel = pModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)invoiceInfoBtnTapped
{
    RBStockOrderInvoiceInfoVC *vc = RBStockOrderInvoiceInfoVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(60) + kRBSafeBottomHeight);
            }];
            
            UIStackView *stackView = UIStackView.new;
            stackView.spacing = Zoom(20);
            stackView.axis = UILayoutConstraintAxisHorizontal;
            stackView.distribution = UIStackViewDistributionFillEqually;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(15));
                make.right.mas_equalTo(-Zoom(15));
                make.top.mas_equalTo(Zoom(10));
                make.height.mas_equalTo(Zoom(40));
            }];
            
            if ([self.response.data.order_status isEqualToString:@"0"])
            {
                [stackView addArrangedSubview:({
                    UIView *view = [self btnWithTitle:@"开票信息" type:0];
                    [view rbAddTapGesWithTarget:self action:@selector(invoiceInfoBtnTapped)];
                    view;
                })];
                [stackView addArrangedSubview:({
                    UIView *view = [self btnWithTitle:@"付款" type:1];
                    [view rbAddTapGesWithTarget:self action:@selector(goPayAction)];
                    view;
                })];
            }
            else
            {
                [stackView addArrangedSubview:({
                    UIView *view = [self btnWithTitle:@"开票信息" type:1];
                    [view rbAddTapGesWithTarget:self action:@selector(invoiceInfoBtnTapped)];
                    view;
                })];
            }
            view;
        });
    }
    return _bottomView;
}

- (UILabel *)btnWithTitle: (NSString *)title type: (NSInteger)type
{
    if (type == 0)
    {
        UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:1 text:title bgColor:UIColor.whiteColor radius:Zoom(20)];
        [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
        return view;
    }
    else
    {
        return [RanTool labelWithFontSize:Zoom(13) weight:1 color:UIColor.whiteColor align:1 text:title bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
    }
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)shopView
{
    UIView *view = UIView.new;
    
    UIImageView *shopImgView = [RanTool imgViewWithMode:1 radius:0 imgName:@"groupPayShop"];
    [view addSubview:shopImgView];
    [shopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(Zoom(19));
        make.height.mas_equalTo(Zoom(17));
        make.left.mas_equalTo(Zoom(16));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *shopNameL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:self.response.data.store_name];
    [view addSubview:shopNameL];
    [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(160));
        make.left.mas_equalTo(shopImgView.mas_right).offset(Zoom(11));
    }];

    return view;
}

- (UIView *)lineView
{
    UIView *view = UIView.new;
    view.backgroundColor = [RBThemeConfig lineGrayColor];
    return view;
}

- (UIView *)goodsViewWithData: (RBOrderDetailModel *)data
{
    UIView *view = UIView.new;
    
    [view h_view_addView:[RanTool imgViewWithMode:2 radius:Zoom(5) imgName:data.file] withHeight:Zoom(97) width:Zoom(97) top:0 bottom:0 left:Zoom(15) right:0 centerY:0];
    [view h_view_addView:({
        UIView *view = UIView.new;
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.goods_name];
            view.numberOfLines = 2;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:({
                UILabel *view = UILabel.new;
                view.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                    [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
                    [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:data.original_price];
                }];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:2 text:[NSString stringWithFormat:@"x%@", data.quantity]];
                [view addPriority:1];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
        if (![data.order_status isEqualToString:@"0"])
        {
            [view view_addView:({
                UIView *view = UIView.new;
                [view h_view_addView:({
                    UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:1 text:@"加工下单" bgColor:UIColor.whiteColor radius:Zoom(15)];
                    [view rbAddTapGesWithTarget:self action:@selector(addOrderBtnTapped)];
                    [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
                    view;
                }) withHeight:Zoom(30) width:Zoom(75) top:0 bottom:0 left:Zoom(12) right:0 centerY:0];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:0 centerX:NO];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:CGFLOAT_MIN left:Zoom(20) right:Zoom(12) centerY:0];
    
    
    return view;
}

- (UIView *)itemWithTitle: (NSString *)title des: (NSString *)des
{
    UIView *view = UIView.new;
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title];
    [titleL addPriority:1];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(Zoom(15));
    }];
    
    if (!des.length)
    {
        des = @"　";
    }
    UILabel *desL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:2 text:des];
    if ([title isEqualToString:@"实付金额"]) {
        desL.textColor = [RBThemeConfig themeColor];
    } else if ([title isEqualToString:@"配送方式"]) {
        desL.textColor = [RBThemeConfig textBlackColor];
    }
    desL.textAlignment = NSTextAlignmentRight;
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(15));
        make.left.mas_equalTo(titleL.mas_right).offset(Zoom(15));
    }];
    
    return view;
}

@end
